<template>
  <div>
    <!-- table区域-begin -->
    <a-table
      ref="table"
      class="j-table-force-nowrap"
      size="middle"
      :scroll="{x: true}"
      bordered
      rowKey="id"
      :columns="columns"
      :components="draggableComponent"
      :dataSource="dataSource"
      :loading="loading"
      :pagination="false"
      @change="handleTableChange"
      :customRow="clickThenCheck">

      <span slot="action" slot-scope="text, record">
        <template v-if="record.taskAssigneeName&&record.taskAssigneeName!=''">
          <a @click.stop="handleProcess(record)">
            办理
          </a>
          <template v-if="record.isForward=='1'">
            <a-divider type="vertical" />
            <a @click.stop="selectForward(record)">
              转发
            </a>
          </template>
          <template v-if="record.isEntrust=='1'">
            <a-divider type="vertical" />
            <a @click.stop="selectEntruster(record)">
              委托
            </a>
          </template>
          <template v-if="record.isTransfer=='1'">
            <a-divider type="vertical" />
            <a @click.stop="selectTransfer(record)">
              转办
            </a>
          </template>
          <!-- 流程重复审批人员跳过， 不允许中途委托。0213-->
          <!--          <a-divider type="vertical" />-->
          <!--          <a @click.stop="selectEntruster(record)">-->
          <!--            委托-->
          <!--          </a>-->
        </template>
        <template v-else>
          <a @click.stop="handleClaimNew(record)" >
            签收
          </a>
          <a-divider type="vertical" />
          <a @click.stop="handleViewDetail(record)">
            查看
          </a>
          <template v-if="record.isIgnore=='1'">
            <a-divider type="vertical" />
            <a @click.stop="handleIgnore(record)">
              忽略
            </a>
          </template>
        </template>

      </span>
      <!-- 字符串超长截取省略号显示-->
      <span slot="bpmBizTitle" slot-scope="text, record">
        <a-icon
          title="催办提醒"
          v-if="record.taskUrge"
          theme="twoTone"
          twoToneColor="#eb2f96"
          @click.stop="taskNotify(record)"
          type="notification"/>
        <span>{{ text }}</span>
      </span>

    </a-table>

    <!-- 弹出框 -->
    <task-deal-modal ref="taskDealModal" :path="path" :formData="formData" @ok="handleOk"></task-deal-modal>
    <select-entruster-modal ref="selectTransferModal" @selectFinished="handleTransfer"></select-entruster-modal>
    <select-forward-modal ref="selectForwardModal" @selectFinished="handleForward"></select-forward-modal>
    <select-entruster-modal ref="selectEntrusterModal" @selectFinished="handleEntruster"></select-entruster-modal>
    <task-notify-me-modal ref="taskNotifyMeModal"></task-notify-me-modal>
    <select-single-user-modal ref="selectSingleUserModal" @selectFinished="selectUserOK"></select-single-user-modal>

    <TaskSignModal ref="taskSignModal" :path="path" :formData="formData" ></TaskSignModal>
    <a-modal v-model="signVisible" title="确认签收吗" :width="450" :maskClosable="false">
      <template slot="footer">
        <a-button type="primary" @click="handleOkNew">确认</a-button>
        <a-button @click="signVisible = false">取消</a-button>
      </template>
      <p><a-icon type="question-circle" style="color: #faad14;margin-right: 5px"/>是否签收该任务?</p>
    </a-modal>
  </div>
</template>

<script>
  import { getAction, putAction } from '@/api/manage'
  import TaskDealModal from '@/views/modules/bpm/task/TaskDealModal';
  import TaskSignModal from '@/views/modules/bpm/task/TaskSignModal'; // 签收
  import SelectEntrusterModal from '@/views/modules/bpm/task/form/SelectEntrusterModal';
  import SelectForwardModal from '@/views/modules/bpm/task/form/SelectForwardModal';
  import JEllipsis from '@/components/jeecg/JEllipsis'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { BpmNodeInfoMixin } from '@/views/modules/bpm/mixins/BpmNodeInfoMixin'
  import TaskNotifyMeModal from '@/views/modules/extbpm/process/TaskNotifyMeModal.vue';
  import SelectSingleUserModal from '@/views/modules/bpm/task/form/SelectSingleUserModal.vue';

  export default {
    props: {
      tableValue: {
        type: String,
        default: ''
      }
    },
    name: 'MyRunningTaskList',
    mixins: [JeecgListMixin, BpmNodeInfoMixin],
    components: {
      SelectSingleUserModal,
      TaskNotifyMeModal,
      SelectEntrusterModal,
      SelectForwardModal,
      TaskDealModal,
      JEllipsis,
      TaskSignModal
    },
    data () {
      return {
        description: '我的任务',
        // 查询条件
        queryParam: {},
        /* 分页参数 */
        ipagination: {
          current: 1,
          pageSize: 6,
          total: 10
        },
        model: {
          userName: ''
        },
        // 表头
        columns: [
          {
            title: '业务标题',
            width: 200,
            ellipsis: true,
            dataIndex: 'bpmBizTitle',
            scopedSlots: { customRender: 'bpmBizTitle' }
          }, {
            title: '流程名称',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'processDefinitionName'
          }, {
            title: '开始时间',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'taskBeginTime'
          }, {
            title: '当前环节',
            align: 'center',
            ellipsis: true,
            dataIndex: 'taskName'
          }, {
            title: '操作',
            align: 'center',
            width: 150,
            dataIndex: 'action',
            fixed: 'right',
            scopedSlots: { customRender: 'action' }
          }
        ],
        path: 'modules/bpm/task/form/FormLoading',
        formData: {},
        url: {
          list: '/api/erpmng/act/task/list',
          claim: '/api/erpmng/act/task/claim',
          ignore: '/api/erpmng/act/task/ignore',
          taskEntrust: '/api/erpmng/act/task/taskEntrust',
          taskForward: '/api/erpmng/act/task/taskForward',
          taskTransfer: '/api/erpmng/act/task/taskTransfer'
        },
        taskId: '',
        bpmBizTitle: '',
        signVisible: false,
        claimModel: {}
      }
    },
    methods: {
      clickThenCheck(record) {
        return {
          on: {
            click: () => {
              if (record.taskAssigneeName && record.taskAssigneeName != '') {
                this.handleProcess(record);
              } else {
                // this.handleClaim(record)
                // this.$refs.taskSignModal.deal()
                this.getProcessNodeInfo(record, 'sign');
              }
            }
          }
        };
      },
      handleOkNew() {
        this.signVisible = false
        this.handleClaim(this.claimModel)
      },
      handleClaimNew(record) {
        this.signVisible = true
        this.claimModel = Object.assign({}, record)
      },
      handleViewDetail(record) {
        if (record.taskAssigneeName && record.taskAssigneeName != '') {
          this.handleProcess(record);
        } else {
          // this.handleClaim(record)
          // this.$refs.taskSignModal.deal()
          this.getProcessNodeInfo(record, 'sign');
        }
      },
      searchReset() {
        this.queryParam = {};
        this.model.userName = '';
        this.loadData(1);
      },
      selectUserOK: function(data) {
        this.model.userName = data.realname;
        this.queryParam.userName = data.username;
      },
      handleSelect: function() {
        this.$refs.selectSingleUserModal.select();
      },
      handleProcess(record) {
        this.getProcessNodeInfo(record);
      },
      handleIgnore(record) {
        var that = this;
        var params = { taskId: record.id };// 查询条件
        this.$confirm({
          title: '确认忽略吗',
          content: '确认忽略后，当前单据将从您的待办中消失。请确认已查看详情，否则，请选择“取消”按钮!',
          cancelText: '确认',
          okText: '取消',
          okButtonProps: {
            props: {
              type: 'default'
            }
          },
          cancelButtonProps: {
            props: {
              type: 'primary'
            }
          },
           // 取消
          onOk: function() {
          },
          // 确认
          onCancel: function() {
            putAction(that.url.ignore, params).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.handleOk();
              } else {
                that.$message.warning(res.message);
                that.handleOk();
              }
            })
          }
        });
      },
      handleClaim(record) {
        var that = this;
        var params = { taskId: record.id };// 查询条件
        this.$confirm({
          title: '确认签收吗',
          content: '请确认已查看详情，否则，请选择“取消”按钮!',
          cancelText: '确认',
          okText: '取消',
          okButtonProps: {
            props: {
              type: 'default'
            }
          },
          cancelButtonProps: {
            props: {
              type: 'primary'
            }
          },
           // 取消
          onOk: function() {
            // putAction(that.url.claim, params).then((res) => {
            //   if (res.success) {
            //     that.$message.success(res.message);
            //     that.handleOk();
            //   } else {
            //     that.$message.warning(res.message);
            //     that.handleOk();
            //   }
            // })
          },
          // 确认
          onCancel: function() {
            putAction(that.url.claim, params).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.handleOk();
              } else {
                that.$message.warning(res.message);
                that.handleOk();
              }
            })
          }
        });
      },
      handleOk() {
        this.loadData();
        this.$emit('ok');
      },
      loadData(arg) {
        if (!this.url.list) {
          this.$message.error('请设置url.list属性!')
          return
        }
        // 加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        var params = this.getQueryParams();// 查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records;
            this.ipagination.total = res.result.total;
            this.$emit('doTotalNum', res.result.total)
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },
      selectEntruster(record) {
        this.taskId = record.id;
        this.$refs.selectEntrusterModal.select(record);
        this.$refs.selectEntrusterModal.title = '选择委托人';
        this.$refs.selectEntrusterModal.message = '委托后，当前待办单据将会转移过去，委托人办理后，办理记录会留在您的已办中。';
      },
      selectTransfer(record) {
        this.taskId = record.id;
        this.$refs.selectTransferModal.select(record);
        this.$refs.selectTransferModal.title = '选择转办人';
        this.$refs.selectTransferModal.message = '转办后，当前待办单据将会转移过去。';
      },
      selectForward(record) {
        this.taskId = record.id;
        this.bpmBizTitle = record.bpmBizTitle;
        this.$refs.selectForwardModal.select(record);
        this.$refs.selectForwardModal.title = '选择转发人';
        this.$refs.selectForwardModal.message = '转发后，当前单据信息会知会给转发人。';
      },
      handleTransfer(data) {
        var that = this;
        var params = {
          taskId: this.taskId,
          taskAssignee: data.username
        };// 查询条件
        console.log('转办', params)
        putAction(that.url.taskTransfer, params).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
            that.loadData();
          }
        })
      },
      handleEntruster(data) {
        var that = this;
        var params = {
          taskId: this.taskId,
          taskAssignee: data.username
        };// 查询条件
        console.log('委托', params)
        putAction(that.url.taskEntrust, params).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.handleOk();
          } else {
            that.$message.warning(res.message);
            that.handleOk();
          }
        })
      },
      handleForward(data) {
        var that = this;
        var params = {
          taskId: this.taskId,
          bpmBizTitle: this.bpmBizTitle,
          taskAssignee: data.username
        };// 查询条件
        putAction(that.url.taskForward, params).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.handleOk();
          } else {
            that.$message.warning(res.message);
            that.handleOk();
          }
        })
      },
      // 催办
      taskNotify(record) {
        this.$refs.taskNotifyMeModal.notify(record);
        this.$refs.taskNotifyMeModal.title = '催办提醒';
      }
    }
  }
</script>
<style lang="less" scoped>
  /** Button按钮间距 */
  .ant-btn {
    margin-left: 3px
  }
</style>
